package org.example.Stream;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.XSlf4j;
import org.example.enums.OrderCanalErrorCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.example.Stream.StreamDemo.getAuthors;

/**
 * @ClassName OptionalTest
 * @Description: TODO
 * @Author: liaosheng
 * @Create: 2025-07-16 14:25
 * @Version 1.0
 **/
public class OptionalTest {

    @Test
    public void test1() {
        // Optional<String> sss = Optional.of("sss");
        // System.out.println(sss.get());
        Optional<String> fff = Optional.of("fff");
    }

    @Test
    public void test2() {
        Optional<String> sss = Optional.ofNullable(null);
        System.out.println(sss.orElse("sff"));
    }

    @Test
    public void test3() {
        Optional<String> sss = Optional.empty();
        System.out.println(sss.orElse("111"));
    }

    @Test
    public void test4() {
        Optional<String> sss = Optional.ofNullable(null);
        sss.ifPresent(System.out::println);
    }


    @Test
    public void test5() {
        Optional<String> sss = Optional.ofNullable(null);
        // sss.get();
        String s = sss.orElse("11");
        System.out.println(s);
        String s1 = sss.orElseGet(() -> "11");
        System.out.println( s1);
        String s2 = sss.orElseThrow(() -> new RuntimeException("11"));


    }

    @Test
    public void test6() {
        List<Author> authors = getAuthors();
        Optional<List<Author>> authors1 = Optional.ofNullable(authors);
        Optional<String> name = Optional.of(authors1.get().get(0).getName());
        // 转大写
        Optional<String> s = name.map(String::toUpperCase);
        // s.ifPresent(System.out::println);
        Optional<Optional<String>> name1 = Optional.of(name);
        //处理嵌套flatMap
        Optional<String> s1 = name1.flatMap(i->i);
        System.out.println(s1.get());
        // sss.get();
        // sss.orElse("11");
        // sss.orElseGet(() -> "11");
        // sss.orElseThrow(() -> new RuntimeException("11"));

    }

    /**
     * Optional 核心功能是处理 null 值
     */
    @Test
    public void test7() {
        List<Author> authors = getAuthors();
        List<Author> collect = authors.stream().filter(item -> StringUtils.isBlank(item.getName())).collect(Collectors.toList());
        // System.out.println(collect+"===");
        Optional<List<Author>> authors1 = Optional.ofNullable(authors);
        Author author = new Author();
        author.setName("");
        Optional<Author> authors2 = Optional.ofNullable(author);
        // authors1.map(CollectionUtils::isEmpty).ifPresent(System.out::println);//判空集合
        // authors2.filter(item ->StringUtils.isBlank(item.getName())).ifPresent(System.out::println);//判空字符串
        authors2.filter(item ->StringUtils.isNotBlank(item.getName())).orElseThrow(RuntimeException::new);// todo map和filter里面不能为空，判空字符串

        //判断个数大于2
        // authors1.filter(i -> i.size() > 2).ifPresent(System.out::println);

    }

    @Test
    public void test8() {
        List<Author> authors = getAuthors();
        Optional<List<Author>> authors1 = Optional.ofNullable(authors);
        //链式调用
        Optional<String> s = authors1.map(i -> i.get(0)).map(Author::getName);
        s.ifPresent(System.out::println);

    }

    @Test
    public void test9() {
        List<Author> authors = getAuthors();
        // 传统方式
        if(authors!=null){
            Author author = authors.get(0);
            if(author!=null){
                System.out.println(author.getName());
            }
        }
        authors=null;
        //新方式
        Optional<List<Author>> authors1 = Optional.ofNullable(authors);
        authors1.map(i -> i.get(0)).map(Author::getName).ifPresent(System.out::println);
    }

    @Test
    public void test10() {
        List<Author> authors = getAuthors();
        Author author =null;
        Optional<Author> author1 = Optional.ofNullable(author);
        Author author2 = author1.orElse(new Author());
        author1.orElseThrow(RuntimeException::new);
        System.out.println(author2.getName());

    }

    @Test
    public void test11() {
        //opitional结合lamda
        List<Author> authors = getAuthors();
        List<Optional<Author>> collect = authors.stream().map(Optional::ofNullable).collect(Collectors.toList());
        List<Author> collect1 = collect.stream()
                .filter(Optional::isPresent)
                .map(Optional::get)
                .collect(Collectors.toList());
        System.out.println(collect1);

    }
    @Test
    public void test12() {
        List<Long> scanCodeIds = new ArrayList<>();
        scanCodeIds.add(1212L);
        scanCodeIds.add(1212L);
        Optional.ofNullable(scanCodeIds)
                .filter(item -> item.contains(1212L))//item代表的是集合本身不是个体，与lamda表达式不一样
                .ifPresent(item -> {
                    throw new RuntimeException();
                });
    }
    // Optional能判空，能过滤，能抛出异常，能判是否存在，能给默认值，能给转换值,for循环只能get，或者else，才可以
    @Test
    public void test13() {
        Author author = new Author();
        author.setName("蒙多1");
        String s = Optional.ofNullable(author)
                .filter(item -> item.getName().equals("蒙多"))//item代表的是个体不是集合本身，与lamda表达式不一样
                .map(Author::getName).orElse("默认值");
                //         .orElseThrow(RuntimeException::new);
                //         .ifPresent(System.out::println);
        System.out.println(s);
    }

    // Optional能判空，能过滤，能抛出异常，能判是否存在，能给默认值，能给转换值,for循环只能get，或者else，才可以,加日志不能用这个option
    @Test
    public void test14() {
        List<Long> scanCodeIds = new ArrayList<>();
        scanCodeIds.add(1212L);
        scanCodeIds.add(1212L);
        Optional.ofNullable(scanCodeIds)
                .filter(item -> item.contains(1212L))//item代表的是集合本身不是个体，与lamda表达式不一样
                .orElse(new ArrayList<>()).forEach(System.out::println);
    }


    @Test
    public void test15() {
        OrderCanalErrorCodeEnum errorCodeEnum = OrderCanalErrorCodeEnum.ORDER_CANAL_ERROR;
        String s = Optional.ofNullable(errorCodeEnum).map(OrderCanalErrorCodeEnum::message).orElse("未知来源");
        System.out.println(s);
    }

    @Test
    public void test16() {
        List<Author> objects = new ArrayList<>();
        List<Author> authors = JSON.parseArray(objects.toString(), Author.class);
        System.out.println(authors);
    }

    // 能用lamda表达式尽量用
    @Test
    public void test17() {
        List<Author> objects = getAuthors();
        objects.forEach(item-> item.setName("11"));
        System.out.println(objects);
    }




}